//
//  NSArray+WXH.m
//  WXHToolKit
//
//  Created by WXH on 16/5/9.
//  Copyright © 2016年 WXH. All rights reserved.
//

#import "NSArray+WXH.h"

@implementation NSArray (WXH)
+ (NSArray *)sortWithArray:(NSArray *)array separator:(NSString *)separator
{
    return [array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSString *path1 = obj1;
        NSString *path2 = obj2;
        
        NSString *name1 = [[path1 lastPathComponent] stringByDeletingPathExtension];
        NSString *name2 = [[path2 lastPathComponent] stringByDeletingPathExtension];
        
        NSArray *array1 = [name1 componentsSeparatedByString:separator];
        NSArray *array2 = [name2 componentsSeparatedByString:separator];
        
//      byHZJ
        //永远把隐藏按钮放在最后面
        if([name1 rangeOfString:@"隐藏"].location != NSNotFound){
            return NSOrderedDescending;
        }
        
        if([name2 rangeOfString:@"隐藏"].location != NSNotFound){
            return NSOrderedAscending;
        }
//      byHZJ
        
        if ([array1 count] < 2 && [array2 count] < 2) {//没有分隔符
            return NSOrderedAscending;
        } else if ([array1 count] > 1 && [array2 count] < 2) {//有分隔符的优先排
            return NSOrderedAscending;
        } else if ([array1 count] < 2 && [array2 count] > 1) {//同上
            return NSOrderedDescending;
        } else {
            NSString *string1 = array1[0];
            NSString *string2 = array2[0];
            
            NSInteger value1 = [string1 integerValue];
            NSInteger value2 = [string2 integerValue];
            
            if (value1 > value2) {
                return NSOrderedDescending;
            }
            return NSOrderedAscending;
        }
    }];
}
@end
